package leetcode.code0414;

/**
 * 执行用时：2 ms, 在所有 Java 提交中击败了91.70%的用户
 *
 * 内存消耗：38.4 MB, 在所有 Java 提交中击败了68.29%的用户
 *
 * 通过测试用例：31 / 31
 *
 * @author CaoYu
 * @date 2021年10月6日
 * @description
 */
public class Solution1 extends Solution414 {

	@Override
	public int thirdMax(int[] nums) {
		Integer num1 = nums[0];
		Integer num0 = null;
		Integer num2 = null;
		for (int i = 1; i < nums.length; i++) {// 0放小,1放大
			int num = nums[i];
			if (num > num1) {
				if (num2 == null) {
					num2 = num;
					continue;
				}
				if (num > num2) {
					num0 = num1;
					num1 = num2;
					num2 = num;
				} else if (num < num2) {
					num0 = num1;
					num1 = num;
				}
			} else if (num < num1) {
				if (num0 == null) {
					num0 = num;
					continue;
				}
				if (num > num0) {
					num0 = num;
				} else if (num2 == null && num < num0) {
					num2 = num1;
					num1 = num0;
					num0 = num;
				}
			}

		}
		return num0 != null && num2 != null ? num0 : num2 != null ? num2 : num1;
	}

	public static void main(String[] args) {
		Solution1 so = new Solution1();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.debug5();
	}

}
